local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog = require "filelog"
local tabletool = require "tabletool"
require "dzpkenum"

local TablesvrHelper = {}
local server

function TablesvrHelper:init(base)
    server = base
end

function TablesvrHelper:get_server()
    return server
end

function TablesvrHelper:sendmsg_to_alltableplayer(msgname, ...)
    local table_data = server.table_data
    local rids = {}
    --通知座位上的玩家
    for _, seat in ipairs(table_data.seats) do
        if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 100 then
            if not seat.waitreenter then
                table.insert(rids, seat.rid)
            end
        end
    end

    for _, wait in pairs(table_data.waits) do
        if wait.rid > 100 and not wait.waitreenter then
            table.insert(rids, wait.rid)
        end
    end
    skynet.send(".agentmgr", "lua", "cmd", "notify", rids, "dzpk." .. msgname, ...)
end

function TablesvrHelper:sendmsg_to_tableplayer(seat, msgname, ...)
    if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 100 then
        skynet.send(".agentmgr", "lua", "cmd", "notify", {seat.rid}, "dzpk." .. msgname, ...)
    end
end

function TablesvrHelper:sendmsg_to_allwaitplayer(msgname, ...)
    local table_data = server.table_data
    local rids = {}
    for rid, wait in pairs(table_data.waits) do
        if wait.rid > 100 then
            table.insert(rids, wait.rid)
        end
    end
    skynet.send(".agentmgr", "lua", "cmd", "notify", rids, "dzpk." .. msgname, ...)
end

function TablesvrHelper:sendmsg_to_waitplayer(wait, msgname, ...)
    skynet.send(".agentmgr", "lua", "cmd", "notify", {wait.rid}, "dzpk." .. msgname, ...)
end

function TablesvrHelper:sendmsg_to_roomsvr(msgname, ...)
    skynet.send(".router", "lua", msgname, ...)
end

function TablesvrHelper:callmsg_to_roomsvr(msgname, ...)
    return skynet.call(".router", "lua", msgname, ...)
end

function TablesvrHelper:sendmsg_to_datasvr(msgname, ...)
    cluster.send("datanode", ".router", msgname, ...)
end

function TablesvrHelper:sendmsg_to_gatesvr(msgname, ...)
    cluster.send("gatenode", ".router", msgname, ...)
end

function TablesvrHelper:callmsg_to_gatesvr(msgname, ...)
    return cluster.call("gatenode", ".router", msgname, ...)
end

function TablesvrHelper:copy_table_gameinfo(gameinfo)
    local table_data = server.table_data
    gameinfo.idx = table_data.idx
    gameinfo.state = table_data.state
    gameinfo.name = table_data.conf.name
    gameinfo.room_type = table_data.conf.room_type
    gameinfo.game_type = table_data.conf.game_type
    gameinfo.max_player_num = table_data.conf.max_player_num
    gameinfo.cur_player_num = table_data.conf.cur_player_num
    -- gameinfo.game_time = table_data.conf.game_time
    gameinfo.create_user_rid = table_data.conf.create_user_rid
    gameinfo.create_user_rolename = table_data.conf.create_user_rolename
    gameinfo.create_time = table_data.conf.create_time
    gameinfo.table_code = table_data.conf.table_code
    gameinfo.action_timeout = table_data.conf.action_timeout
    gameinfo.create_user_logo = table_data.conf.create_user_logo
    gameinfo.min_carry_diamond = table_data.conf.min_carry_diamond
    gameinfo.leave_carry_diamond = table_data.conf.leave_carry_diamond
    gameinfo.base_score = table_data.conf.base_score
    gameinfo.max_score = table_data.conf.max_score
    gameinfo.action_seat_index = table_data.action_seat_index
    gameinfo.action_to_time = table_data.action_to_time
    gameinfo.action_type = table_data.action_type
    gameinfo.is_public = table_data.conf.is_public
    gameinfo.curr_round_code = table_data.conf.curr_round_code
    gameinfo.allchip = table_data.allbets

    gameinfo.seats = {}
    gameinfo.player_infos = {}
    local seatinfo, playerinfo
    for index, seat in pairs(table_data.seats) do
        seatinfo = {}
        playerinfo = {}
        self:copy_seatinfo(seatinfo, seat)
        table.insert(gameinfo.seats, seatinfo)
        self:copy_playerinfo(playerinfo, seat)
        table.insert(gameinfo.player_infos, playerinfo)
    end
end

function TablesvrHelper:copy_seatinfo(seatinfo, seat)
    local table_data = server.table_data
    seatinfo.rid = seat.rid
    seatinfo.index = seat.index
    seatinfo.state = seat.state
    seatinfo.is_tuoguan = seat.is_tuoguan
    seatinfo.diamond = seat.diamond
    seatinfo.is_ready = seat.is_ready
    seatinfo.ready_to_time = seat.ready_to_time
    seatinfo.round = table_data.betcount
    seatinfo.chip = seat.bets
    seatinfo.curr_chip = seat.curr_bets
end

function TablesvrHelper:copy_playerinfo(playerinfo, seat)
    playerinfo.rid = seat.rid
    playerinfo.rolename = seat.playerinfo.rolename
    playerinfo.logo = seat.playerinfo.logo or ''
    playerinfo.sex = seat.playerinfo.sex or 0
    playerinfo.ip_addr = seat.playerinfo.ip_addr or ''
    playerinfo.fangka = seat.playerinfo.fangka or 0
    playerinfo.diamond = seat.playerinfo.diamond or 0
    playerinfo.coin = seat.playerinfo.coin or 0
    playerinfo.introduce = seat.playerinfo.introduce or ''
    playerinfo.gps_addr = seat.playerinfo.gps_addr
    playerinfo.logoframe = seat.playerinfo.logoframe or 100
end

return  TablesvrHelper